/*******************************************************************************
  MPLAB Harmony Application Source File

  Company:
    Microchip Technology Inc.

  File Name:
    app.c

  Summary:
    This file contains the source code for the MPLAB Harmony application.

  Description:
    This file contains the source code for the MPLAB Harmony application.  It
    implements the logic of the application's state machine and it may call
    API routines of other MPLAB Harmony modules in the system, such as drivers,
    system services, and middleware.  However, it does not call any of the
    system interfaces (such as the "Initialize" and "Tasks" functions) of any of
    the modules in the system or make any assumptions about when those functions
    are called.  That is the responsibility of the configuration-specific system
    files.
 *******************************************************************************/

// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************

#include "app.h"
#include "definitions.h"    //必須      

#include "stdio.h"
#include "1lcd_lib_XC32.h"
#include "INT035_Touch_lib_XC32.h"

int adcX,adcY;  //タッチ座標x,y
int cTouched;   //タッチ発生で1
int prevTouched;    //一巡前の　タッチ発生で '1'、　タッチ無なら '0'
int prev_abcX, prev_abcY; 
int delay_Clock = 200000000;   //システムクロック：200MHz
char Buf[64];


void delay_us(volatile unsigned int usec)        //1μsec遅延
{
        volatile  int count;
        count = (int)(delay_Clock/20000000)*usec;
        
        do      //実測　at　200MH (Clock=200000000)
        {       //delay_us(1000)：1000.4μsec  delay_us(100)：100.6μsec  delay_us(10):10.5μsec　　delay_us(1)：1.5μsec
                asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");asm("NOP");
                asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");

                count--;
        }while(count != 0);
}

void delay_ms(volatile unsigned int msec)        //1msec遅延
{
        volatile unsigned int i;         //実測：at200MH (Clock=200000000)//delay_ms(1)： 1.0006msec   delay_ms(100)：100.04msec

        for(i=0; i<msec; i++)
        delay_us(1000);
}
     
void ys_DRV_TOUCH_INT035(void)    //MSP2807タッチ検出  //タッチ検出、タッチ種類、タッチ位置とシステムに注入
{
    cTouched = INT035_TouchDetect();        //タッチ検出で　'1'     //Position detedted
    adcX = ys_TouchGetX();  //タッチ位置のx座標
    adcY = ys_TouchGetY();  //タッチ位置のy座標
   
     if((prevTouched == 0) && (cTouched == 1)) // Touch: Pressed
     {
         SYS_INP_InjectTouchDown(0,adcX,adcY);  //Touch:Pressed をシステムに注入
     }
    
     if(prevTouched == 1)   //一巡前のタッチが有った場合
     {
        if(cTouched == 1)                 // Touch: Moved
        {
            SYS_INP_InjectTouchMove(1,adcX,adcY);   //Touch:Movedをシステムに注入
        }
        else                        //Touch: Released
        {
            SYS_INP_InjectTouchUp(0,prev_abcX,prev_abcY);     //touch:Upをシステムに注入
        }
     }
    
    ////前回の値を保持
     prevTouched = cTouched;       
     prev_abcX = adcX;
     prev_abcX = adcY; 
     
    //タッチ位置座標のキャラクタ液晶への表示 
     	if(cTouched)    //タッチ有無判定			
		{
            if(adcX<320 && adcY<240) //座標表示範囲: (0,0)-(320,240)の矩形を指定
			{
                    //キャラクタ液晶にタッチ位置座標を表示
                    lcd_cmd(0x80);          //1目の先頭へ
                    sprintf(Buf,"Touch Position             \r\n");//
                    //sprintf(Buf,"cTouched=%d              \r\n",cTouched);//
                    lcd_str(Buf);                   //液晶表示
                   
                    lcd_cmd(0xC0);          //２行目の先頭へ
                    sprintf(Buf,"X=%d, Y=%d                 \r\n",adcX, adcY);	//
                    lcd_str(Buf);           // 開始メッセージ1行目表示
            }
		}else     	
        { 
            delay_ms(10);
        } 
}


// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************

// *****************************************************************************
/* Application Data

  Summary:
    Holds application data

  Description:
    This structure holds the application's data.

  Remarks:
    This structure should be initialized by the APP_Initialize function.

    Application strings and buffers are be defined outside this structure.
*/

APP_DATA appData;

// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Functions
// *****************************************************************************
// *****************************************************************************

/* TODO:  Add any necessary callback functions.
*/

// *****************************************************************************
// *****************************************************************************
// Section: Application Local Functions
// *****************************************************************************
// *****************************************************************************


/* TODO:  Add any necessary local functions.
*/


// *****************************************************************************
// *****************************************************************************
// Section: Application Initialization and State Machine Functions
// *****************************************************************************
// *****************************************************************************

/*******************************************************************************
  Function:
    void APP_Initialize ( void )

  Remarks:
    See prototype in app.h.
 */

void APP_Initialize ( void )
{
    /* Place the App state machine in its initial state. */
    appData.state = APP_STATE_INIT;



    /* TODO: Initialize your application's state machine and other
     * parameters.
     */
    
    ys_TouchHardwareInit(); //INT035のタッチ制御ライブラリ関数初期化
      
          
    lcd_init();				// LCD初期化
    lcd_cmd(0b00001100);    // カーソル:OFF  ブﾘﾝｸ:OFF
    lcd_clear();
    
    lcd_cmd(0x80);          //1目の先頭へ
    sprintf(Buf,"Touch Position             \r\n");//
    lcd_str(Buf);                   //液晶表示
    
    lcd_cmd(0xC0);          //２行目の先頭へ
    sprintf(Buf,"X=   , Y=                   \r\n");	//
    lcd_str(Buf);           // 開始メッセージ1行目表示
    
    delay_ms(1000);
    
}


/******************************************************************************
  Function:
    void APP_Tasks ( void )

  Remarks:
    See prototype in app.h.
 */

void APP_Tasks ( void )
{

    /* Check the application's current state. */
    switch ( appData.state )
    {
        /* Application's initial state. */
        case APP_STATE_INIT:
        {
            bool appInitialized = true;


            if (appInitialized)
            {

                appData.state = APP_STATE_SERVICE_TASKS;
            }
            break;
        }

        case APP_STATE_SERVICE_TASKS:
        {

            break;
        }

        /* TODO: implement your application state machine.*/


        /* The default state should never be executed. */
        default:
        {
            /* TODO: Handle error in application's state machine. */
            break;
        }
    }
}


/*******************************************************************************
 End of File
 */
